<?php
require_once 'Log.php';
/**
 * Created by PhpStorm.
 * User: zpr
 * Date: 2017/6/27
 * Time: 18:22
 */
class Cache{
    public $cacheFile = "cache.json"; //文件
    public $dir = "d://"; //目录
    public $expires_time = 3600;

    //缓存
    public function setCache($name, $val){
        $file = $this->hasFile();
        //字符串转数组
        $str = file_get_contents($file);
        $arr = json_decode($str, true);

        //值为空，则移除该缓存
        if(empty($val)){
            unset($arr[$name]);
        }else{
            $arr[$name] = array("value"=>$val, "expires_time"=>$this->expires_time, "add_time"=>time());
        }
        //数组转字符串
        $str = json_encode($arr);
        file_put_contents($file, $str);
    }
    public function getCache($name){
        try{
            $file = $this->hasFile();
            //字符串转数组
            $str = file_get_contents($file);
            $allArr = json_decode($str, true);
            $count = sizeof($allArr);
            if($count==0){
                return false;
            }
            $arr = $allArr[$name];

            if(!$arr || time() > ($arr["expires_time"] + $arr["add_time"])){
                $this->removeCache($name); //过期移除
                return false;
            }
        }catch (Exception $e){
            Log::outLog("Cache",$e);
            return false;
        }
        return $arr["value"];
    }
    public function removeCache($name){
        $this->setCache($name, '', 0);
    }

    private function hasFile(){
        //如果不存在缓存文件，则创建一个
        if(!file_exists($this->dir)){
            mkdir($this->dir);
        }
        if(!file_exists($this->dir . $this->cacheFile)){
            touch($this->dir . $this->cacheFile);
        }
        return $this->dir . $this->cacheFile;
    }
}
